package com.git.groovy.controller;

import com.git.groovy.groovyScript.TestGroovyService;
import com.git.groovy.service.GroovyScriptService;
import com.git.groovy.service.ITestService;
import com.git.groovy.service.MyTestService;
import com.git.groovy.utils.GroovyUtil;
import com.git.groovy.utils.SpringContextUtils;
import groovy.lang.GroovyClassLoader;
import groovy.util.GroovyScriptEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    GroovyScriptService groovyScriptService;

    @Autowired
    TestGroovyService testGroovyService;

    @Autowired
    private ApplicationContext applicationContext;


    /**
     * spring中直接用Groovy开发
     * @return
     */
    @RequestMapping("testGroovyService")
    public String testGroovyService(){
        String hi = testGroovyService.sayHello("Hi");
        return hi;
    }

    /**
     * 不可用，因未注入到Spring的ioc中
     * @return
     */
    @RequestMapping("testSayHello")
    public String testSayHello(){
        ITestService iTestService = SpringContextUtils.getBean(ITestService.class);
        String str1 = iTestService.sayHello("testSayHello");
        String str2 = iTestService.sayHelloTestSpring("testSayHello");
        return str1 + str2;
    }

    @RequestMapping("testForGit")
    public String testForGit() throws IllegalAccessException, IOException, InstantiationException {
        ITestService iTestService = groovyScriptService.getGroovyScriptEngine("/TestGroovyService.groovy");
        String strRes = iTestService.sayHello("aaa");
        System.out.println(strRes);
        return strRes;
    }

    @RequestMapping("updateScriptForGit")
    public String updateScriptForGit() {
        Boolean isSuccess  = groovyScriptService.updateScript();
        return "res:" + isSuccess;
    }

    /**
     * 测试从String中加载类
     */
    @RequestMapping("loaderGroovyByStrTest")
    public String loaderGroovyTest() throws IllegalAccessException, InstantiationException, IOException {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        String str = "import com.git.groovy.service.ITestService\n" +
                "\n" +
                "public class TestGroovyService implements ITestService{\n" +
                "\n" +
                "    String sayHello(String str) {\n" +
                "        println str;\n" +
                "        return \"HI \" + str;\n" +
                "    }\n" +
                "}";

        Class groovyClass  = groovyClassLoader.parseClass(new File("E:\\work\\git-oschina\\javaProject\\alan.springboot\\gitGroovyDemo\\src\\main\\resources\\GroovyScript\\TestGroovyService.groovy"));
//        Class groovyClass  = groovyClassLoader.parseClass(str,"TestGroovyService.groovy");
        ITestService iTestService = (ITestService) groovyClass.newInstance();
        String strRes = iTestService.sayHello("aaa");
        System.out.println(strRes);
        return strRes;
    }

    /**
     * 测试加载工作类
     */
    @RequestMapping("loaderGroovyByUtil")
    public String loaderGroovyByUtil() throws IllegalAccessException, IOException, InstantiationException {
        ITestService iTestService = GroovyUtil.loaderClass("E:\\work\\git-oschina\\javaProject\\alan.springboot\\gitGroovyDemo\\src\\main\\resources\\GroovyScript\\TestGroovyService.groovy");
        String strRes = iTestService.sayHello("aaa");
        System.out.println(strRes);
        return strRes;
    }

    /**
     * 测试是否能成功注入Bean&接口
     */
    @RequestMapping("loaderGroovySpringByUtil")
    public String loaderGroovySpringByUtil() throws IllegalAccessException, IOException, InstantiationException {
        ITestService iTestService = GroovyUtil.loaderClassAndSpring("E:\\work\\git-oschina\\javaProject\\alan.springboot\\gitGroovyDemo\\src\\main\\resources\\GroovyScript\\TestGroovyService.groovy");
        String strRes = iTestService.sayHelloTestSpring("aaa");
        System.out.println(strRes);
        return strRes;
    }

    /**
     * 测试是否能成功注入Bean
     */
    @RequestMapping("loaderGroovySpringByUtil2")
    public String loaderGroovySpringByUtil2() throws IllegalAccessException, IOException, InstantiationException {
        MyTestService myTestService = GroovyUtil.loaderClassAndSpring("E:\\work\\git-oschina\\javaProject\\alan.springboot\\gitGroovyDemo\\src\\main\\resources\\GroovyScript\\MyTestGroovyService.groovy");
        String strRes = myTestService.testHello("aaa");
        System.out.println(strRes);
        return strRes;
    }

    /**
     * 测试@value
     */
    @RequestMapping("loaderGroovySpringByUtil3")
    public String loaderGroovySpringByUtil3() throws IllegalAccessException, IOException, InstantiationException {
        MyTestService myTestService = GroovyUtil.loaderClassAndSpring("E:\\work\\git-oschina\\javaProject\\alan.springboot\\gitGroovyDemo\\src\\main\\resources\\GroovyScript\\MyTestGroovyService2.groovy");
        String strRes = myTestService.testHello("aaa");
        System.out.println(strRes);
        return strRes;
    }

    public static void main(String[] args) throws IllegalAccessException, IOException, InstantiationException {
        TestController testController = new TestController();
        testController.loaderGroovyByUtil();
    }

}
